package com.android.videotest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * 手机尺寸工具类
 * <p>
 * Created by WangYaDong on 2016/2/25.
 */
public class DimensionUtils {

    private static float sDensity;
    private static int sScreenWidth;
    private static int sScreenHeight;
    private static int sStatusBarHeight;

    static {
        Context context = App.getApp();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (null != wm) {
            wm.getDefaultDisplay().getMetrics(dm);
            sDensity = dm.density;
            sScreenWidth = dm.widthPixels;
            sScreenHeight = dm.heightPixels;
        }

        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object dimen = clazz.newInstance();
            int id = Integer.parseInt(clazz.getField("status_bar_height").get(dimen).toString());
            sStatusBarHeight = context.getResources().getDimensionPixelSize(id);
        } catch (Exception e) {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                sScreenHeight = context.getResources().getDimensionPixelSize(resourceId);
            }
        }
    }

    private DimensionUtils() {

    }

    public static int getPixelFromDp(float value) {
        return (int) (value * sDensity + 0.5);
    }

    public static float getDensity() {
        return sDensity;
    }

    public static int getScreenWidth() {
        return sScreenWidth;
    }

    public static int getScreenHeight() {
        return sScreenHeight;
    }

    public static int getStatusHeight() {
        return sStatusBarHeight;
    }

    public static int getScreenHeightWithoutStatusBar(Activity a) {
        Rect frame = new Rect();
        a.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return getScreenHeight() - frame.top;
    }
}
